package com.aletto.general_store.presenters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aletto.general_store.GeneralStoreApplication;
import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderDetails;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;

import java.util.List;

public class ConsumerOrderDetailsTemplate extends SimpleTemplate<OrderDetails> {

    private final String id;

    private final ViewGroup parent;

    public ConsumerOrderDetailsTemplate(String id, ViewGroup parent) {
        this.id = id;
        this.parent = parent;
    }

    @Override
    public INetTask.View<OrderDetails> view() {
        return new ToastView<>(GeneralStoreApplication.sInstance, providerView());
    }

    private INetTask.View<OrderDetails> providerView() {
        TextView tv_address = parent.findViewById(R.id.tv_address);
        TextView tv_mobile_number = parent.findViewById(R.id.tv_mobile_number);
        TextView tv_shop_name = parent.findViewById(R.id.tv_shop_name);
        TextView tv_goods_title = parent.findViewById(R.id.tv_goods_title);
        TextView tv_sold_number = parent.findViewById(R.id.tv_sold_number);
        TextView tv_goods_price = parent.findViewById(R.id.tv_goods_price);
        TextView tv_order_number = parent.findViewById(R.id.tv_order_number);
        TextView tv_pay_time = parent.findViewById(R.id.tv_pay_time);
        TextView tv_pay_channel = parent.findViewById(R.id.tv_pay_channel);
        TextView tv_pay_hash = parent.findViewById(R.id.tv_pay_hash);
        ImageView iv_shop_logo = parent.findViewById(R.id.iv_shop_logo);
        ImageView iv_goods_cover = parent.findViewById(R.id.iv_goods_cover);

        return new INetTask.View<>() {
            @Override
            public void success(OrderDetails data) {
                // 店铺
                OrderDetails.ShopDTO shopDTO = data.getShop();
                tv_shop_name.setText(shopDTO.getName());
                GlideApp.with(iv_shop_logo).load(shopDTO.getHead_img()).into(iv_shop_logo);

                // 地址
                OrderDetails.AddressDTO addressDTO = data.getAddress();
                tv_address.setText(addressDTO.getAddress());
                tv_mobile_number.setText(addressDTO.getName() + "\t\t\t" + addressDTO.getPhone());
                parent.findViewById(R.id.layout_address).setVisibility(data.getGoods_type() == 1 ? View.GONE : View.VISIBLE);

                // 商品
                List<String> goodsImg = data.getGoods_img();
                if (goodsImg != null && !goodsImg.isEmpty()) {
                    GlideApp.with(iv_goods_cover).load(goodsImg.get(0)).into(iv_goods_cover);
                }

                tv_goods_title.setText(data.getGoods_name());
                tv_sold_number.setText("× " + data.getNum());
                tv_goods_price.setText("实付：" + data.getPrice() + " USDT");

                tv_order_number.setText(data.getOrder_id());
                XHelper.copy2Clipboard(tv_order_number.getText());

                tv_pay_time.setText(data.getPay_time());
                tv_pay_channel.setText(data.getPay_network());

                tv_pay_hash.setText(data.getPay_hash());
                XHelper.copy2Clipboard(tv_pay_hash.getText());
            }
        };
    }

    @Override
    public INetTask.Presenter<OrderDetails, INetTask.View<OrderDetails>> presenter() {
        return new NetSingleTask<>(DataRepository.getInstance().orderDetails(id));
    }

}
